package com.fitsoftwareprocess.mobilemap;

import java.util.ArrayList;

import com.fitsoftwareprocess.mobimap.dto.Coordinate;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
//import android.support.v4.app.NavUtils;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;

public class MainActivity extends TabActivity implements TabHost.TabContentFactory{
	
	Intent intent; 
	RadioButton rbFirst;   
    RadioButton rbSecond ; 
     
    RadioButton rbThird ;
    RadioButton rbFourth ;
    String longitude="";
    String latitude="";
    static public String mylongitude="";
    static public String mylatitude="";
    LocationManager locationManager;
    Location userLocation;
    
    
    RadioButton[] rdTabs = new RadioButton[4];
	private int mapType = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
		String  provider = LocationManager.GPS_PROVIDER;
		//userLocation = new Location("");
		userLocation = locationManager.getLastKnownLocation(provider);
		
		LocationListener locationListener = new LocationListener() {
			
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				userLocation.setLatitude(location.getLatitude());
				userLocation.setLongitude(location.getLongitude());
				
			}
		};
		locationManager.requestLocationUpdates(provider, 0, 0,locationListener);

        TabHost tabHost = getTabHost();  
        
        
        intent = new Intent().setClass(this, HomeActivity.class);
        
        
        if(getIntent().getExtras()!=null)
        {
  		  longitude=getIntent().getExtras().getString("long");
  		  mylongitude = longitude;
  		  latitude=getIntent().getExtras().getString("lat");
  		  mylatitude = latitude;
  		  ArrayList<Coordinate> direction = (ArrayList<Coordinate>)getIntent().getExtras().getSerializable("direction");
  		  intent.putExtra("long",longitude);
  		  intent.putExtra("lat", latitude);
  		  intent.putExtra("direction", direction);
  		  intent.putExtra("mapType", mapType);
        }
       
        	
        tabHost.addTab(tabHost.newTabSpec("home").setIndicator("Home").setContent(intent));
        intent = new Intent().setClass(this, AddressActivity.class);
        tabHost.addTab(tabHost.newTabSpec("address").setIndicator("Address").setContent(intent));
        intent = new Intent().setClass(this,StreetActivity.class);
        tabHost.addTab(tabHost.newTabSpec("street").setIndicator("Street").setContent(intent));
        intent = new Intent().setClass(this, ServiceActivity.class);
        tabHost.addTab(tabHost.newTabSpec("service").setIndicator("Service").setContent(intent))  ;
        
        setupUI();
        setTab(0);
    }
    @Override
	public boolean onCreateOptionsMenu(Menu menu) 
    { 
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.activity_main, menu);
    	return true;
	}
    @Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
	// Handle item selection
		//Intent temp;
		switch (item.getItemId()) 
		{
			case R.id.myaddress:// tim vi tri dang dung
				
				longitude=userLocation.getLongitude()+"";
		  		latitude=userLocation.getLatitude()+"";
		  		intent = new Intent(MainActivity.this,MainActivity.class);
				intent.putExtra("long",longitude);
				intent.putExtra("lat",latitude);
				startActivity(intent);
				break;
			case R.id.findservice:// tim dich vu xung quanh
				
				intent = new Intent(MainActivity.this, ServiceAround.class);
				intent.putExtra("long", mylongitude);
				intent.putExtra("lat", mylatitude);
				startActivity(intent);
				break;
				
			case R.id.addservice:// them dich vu
				intent = new Intent(MainActivity.this,AddServiceActivity.class);
				intent.putExtra("long", mylongitude);
				intent.putExtra("lat", mylatitude);
				startActivity(intent);
				break;
			
		}
		return super.onOptionsItemSelected(item);
	}

 

	public View createTabContent(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	private void setTab(int index)
	{
		getTabHost().setCurrentTab(index);
		
		for(int i = 0;i<rdTabs.length;i++)
		{
			if(i == index)
			{
				rdTabs[i].setBackgroundColor(Color.TRANSPARENT);
			}
			else
			{
				rdTabs[i].setBackgroundColor(Color.TRANSPARENT);
			}
		}
	}
	private void setupUI() {

		
		rdTabs[0] = (RadioButton) findViewById(R.id.first);
         
		rdTabs[1] = (RadioButton) findViewById(R.id.second); 
         
		rdTabs[2] = (RadioButton) findViewById(R.id.third);
		rdTabs[3] = (RadioButton) findViewById(R.id.fourth);

         
		rdTabs[0].setButtonDrawable(R.drawable.home);
		rdTabs[1].setButtonDrawable(R.drawable.address);  
		rdTabs[2].setButtonDrawable(R.drawable.street);
		rdTabs[3].setButtonDrawable(R.drawable.service);
	
        
		for(int i = 0;i<rdTabs.length;i++)
		{
			if(i == 0)
			{
				rdTabs[i].setBackgroundColor(Color.LTGRAY);
			}
			else
			{
				rdTabs[i].setBackgroundColor(Color.DKGRAY);
			}
		}
         
        RadioGroup rg = (RadioGroup) findViewById(R.id.states);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, final int checkedId) {

                switch (checkedId) {
                
                case R.id.first:
                {
                    setTab(0);
                    break;
                }
                case R.id.second:
                {

                    setTab(1);
                    break;
                }
                case R.id.third:
                {

                    setTab(2);
                    break;
                }
                case R.id.fourth:
                {

                    setTab(3);
                    break;
                }
         
                
                }
            }
        });
	}


}
